home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / freeware / openvip.exe / {app} / xmlhelpers.py < prev    next >
Encoding:
Python Source  |  2003-06-03  |  1.4 KB  |  50 lines

  1. #
  2. # This file is part of OpenVIP (http://openvip.sourceforge.net)
  3. #
  4. # Copyright (C) 2002-2003
  5. # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
  6. #
  7. # This program is licensed under GNU General Public License version 2;
  8. # see file COPYING in the top level directory for details.
  9. #
  10. # $Id: xmlhelpers.py,v 1.3 2003/06/03 21:01:13 vaclavslavik Exp $
  11. #
  12. # Utilities to help with XML processing, based on libxml2. Mainly validation
  13. # and output formatting.
  14. #
  15.  
  16. HAVE_LIBXML2 = True
  17. try:
  18.     import libxml2
  19.     libxml2.initializeCatalog()
  20.     libxml2.loadCatalog('data/dtd/openvip-catalog.xml')
  21.     #libxml2.setEntityLoader(libxml2.xmlNoNetExternalEntityLoader)
  22. except ImportError:
  23.     print 'libxml2 missing, will not validate inputs'
  24.     HAVE_LIBXML2 = False
  25.  
  26.  
  27. def validate(filename):
  28.     """Validates XML file against DTD."""
  29.     if not HAVE_LIBXML2: return True
  30.     ctxt = libxml2.createFileParserCtxt(filename)
  31.     ctxt.validate(1)
  32.     ctxt.parseDocument()
  33.     doc = ctxt.doc()
  34.     valid = ctxt.isValid()
  35.     doc.freeDoc()
  36.     return valid
  37.  
  38.  
  39. def formatNicely(filename):
  40.     """Reformats XML file to look good - in particular, add indentation.
  41.        'filename' must point to existing valid XML file that is readable
  42.        (/dev/null won't do)."""
  43.     if not HAVE_LIBXML2: return True
  44.     ctxt = libxml2.createFileParserCtxt(filename)
  45.     ctxt.parseDocument()
  46.     doc = ctxt.doc()
  47.     doc.saveFormatFile(filename, 1)
  48.     doc.freeDoc()
  49.     return True
  50.